class Node {
    public int val;
    public Node next;//null

    public Node(int val) {
        this.val = val;
    }
}
//链表
public class MyLinkedList {
    public Node head;//标识单链表的头节点

    //打印链表
    public void show() {
        Node cur=this.head;
        System.out.print("打印链表：");
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }

    //得到单链表的长度
    public int size() {
        int count=0;
        Node cur=this.head;
        while(cur!=null){
            cur=cur.next;
            count++;
        }
        return count;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node cur=this.head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }



    //头插法
    public void addFirst(int data) {
        Node Data=new Node(data);
        if(this.head==null){
            this.head=Data;
            return ;
        }
        Data.next=this.head;
        this.head=Data;
    }
    //尾插法
    public void addLast(int data) {
        Node Data=new Node(data);
        if(this.head==null) {
            this.head = Data;
            return ;
        }
        Node cur=this.head;
        while(cur.next!=null){
            cur=cur.next;
        }
        cur.next=Data;
    }
    //找到要插入下标的前一个节点
    public Node searchPrev(int index) {
        Node cur=this.head;
        int count=0;
        while(count!=index-1){
            cur=cur.next;
            count++;
        }
        return cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index<0||index>size()){
            System.out.println("下标不合法，请重试");
            return;
        }
        if(index==0){
            addFirst(data);
            return;
        }
        else if(index==size()){
            addLast(data);
        }
        Node cur=searchPrev(index);
        Node Data=new Node(data);
        Data.next=cur.next;
        cur.next=Data;
    }


    //查看链表中是否有key节点
    public Node searchPrevNode(int key) {
        Node cur=this.head;
        while(cur.next!=null){
            if(cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(this.head==null){
            System.out.println("链表为空，无法删除");
            return;
        }
        if(this.head.val==key){
            this.head=this.head.next;
            return ;
        }
        Node cur=searchPrevNode(key);
        if(cur==null){
            System.out.println("没有该关键字，无法删除");
            return ;
        }
        Node tmp=cur.next;
        cur.next=tmp.next;
    }


    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(this.head==null){
            System.out.println("链表为空，无法删除");
            return ;
        }
        Node prev=this.head;
        Node cur=this.head.next;
        while(cur!=null){
            if(cur.val==key){
                prev.next=cur.next;
                cur=cur.next;
            }
            else{
                prev=cur;
                cur=cur.next;
            }
        }
        if(this.head.val==key){
            this.head=this.head.next;
        }
    }
    //释放清空链表
    public void clear(){
        while(this.head!=null){
            Node cur=this.head.next;
            this.head.next=null;
            this.head=cur;
        }
    }


    //删除链表中等于给定值 val 的所有节点
    public void removeAllval(int val) {
        if(this.head==null){
            System.out.println("链表为空，无法删除");
            return ;
        }
        Node prev=this.head;
        Node cur=this.head.next;
        while(cur!=null){
            if(cur.val==val){
                prev.next=cur.next;
                cur=cur.next;
            }
            else{
                prev=cur;
                cur=cur.next;
            }
        }
        if(this.head.val==val){
            this.head=this.head.next;
        }
    }

    //反转一个单链表
    public Node reverseList() {
        Node Nodenext=null;
        Node cur=this.head;
        while(cur!=null){
            Node perv=cur.next;
            cur.next=Nodenext;
            Nodenext=cur;
            cur=perv;
        }
        return Nodenext;
    }
    //返回链表的中间结点
    public Node middleNode() {
        Node fast=this.head;
        Node slow=this.head;
        while(slow.next!=null&&slow!=null){
            slow=slow.next.next;
            fast=fast.next;
        }
        return fast;
    }



    //输入一个链表，输出该链表中倒数第k个结点
    public Node FindKthToTail(int k) {
        Node fast=this.head;
        Node slow=this.head;
        while(fast!=null){
            if(k<=0){
                slow=slow.next;
            }
            fast=fast.next;
            k--;
        }
        return k<=0?slow:null;
    }

    //合并两个有序链表
    public Node mergeTwoLists(Node headA, Node headB) {
        if(headA==null){
            return headB;
        }
        if(headB==null){
            return headA;
        }
        if(headA==null && headB==null){
            return null;
        }
        Node prev=new Node(-1);
        Node sur=prev;
        while(headA!=null&&headB!=null){
            if(headA.val>headB.val){
                sur.next=headB;
                headB=headB.next;
            }
            if(headA.val<headB.val){
                sur.next=headA;
                headA=headA.next;
            }
            sur=sur.next;
        }
        if(headA==null){
            sur.next=headB;
        }
        if(headB==null){
            sur.next=headA;
        }
        return prev.next;
    }
}
